/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 

function buco() { 
}

buco.start = function() { 	
	// Initialize toolbar
	$(function() {
		$( "#newButton" ).button({			
			icons: {
				primary: "ui-icon-document"
			}
		});
		$( "#addButton" ).button({			
			icons: {
				primary: "ui-icon-plus"
			}
		});
		$( "#removeButton" ).button({			
			icons: {
				primary: "ui-icon-minus"
			}
		});
		$( "#undoButton" ).button({			
			icons: {
				primary: "ui-icon-arrowreturn-1-w"
			}
		});						
		$( "#redoButton" ).button({			
			icons: {
				primary: "ui-icon-arrowreturn-1-e"
			}
		});					
		$( "#integrityButton" ).button({			
		icons: {
			primary: "ui-icon-search"
		}
		});		
		$( "#deployButton" ).button({			
		icons: {
			primary: "ui-icon-folder-open"
		}
		});			
		$( "#importButton" ).button({			
		icons: {
			primary: "ui-icon-arrowthick-1-n"
		}
		});				
		$( "#exportButton" ).button({			
		icons: {
			primary: "ui-icon-arrowthick-1-s"
		}
		});			
	});
	
	$(document).ready(function () {
		// Init layout
		buco.mainLayout = $("body").layout({
			applyDefaultStyles: true, 			
			north__size: 50, 
			north__resizable: false, 
			north__closable: false, 
			north__spacing_open: 0,
			south__spacing_open: 4,			
			south__initClosed: false,
			south__size: 150,
			east__size: 400,
			east__spacing_open: 4			
		});		
		
		buco.propertyLayout = $("#propertyLayout").layout({			
			applyDefaultStyles: true, 											
			center__paneSelector:"#propertyLayout",			
			south__size: 250,
			south__initClosed: false,
		});
				
		// Init dialogs, but do not open 
		$("#addBundleDialog").dialog({autoOpen: false, modal: true});		
		$("#importDialog").dialog({autoOpen: false, modal: true});		
		
		// AJAX notification 
		$(this).ajaxStart(function() {
			$("#ajaxNotification").show();
		});
		
		$(this).ajaxStop(function() {
			$("#ajaxNotification").hide();
		});
				
		// We're using jQuery UI Buttons 
		$("input:submit, button").button();
		
		// When a button is clicked a controller action should be executed
		$(".button").click(function() { 	
			var action = $(this).attr("action");	
			if(action != null) { 
				buco.mvc.executeAction(action); 		
			}
		}); 
		
		$("#uploadJarFileButton").click(function() { 			
			buco.mvc.executeAction("composition.addBundle");
		}); 
		
		$("#uploadImportFileButton").click(function() { 			
			buco.mvc.executeAction("composition.importComposition");
		}); 
				
		// Load Composition 
		buco.mvc.executeAction("composition.load"); 			
		
		jQuery.jstree._themes = "/js/lib/jstree_themes/";	
	});
}(); 

// For Firebug logging
buco.log = function(err) {
    if(console != null && console.log != null) { 
		console.log(err); 
	}
}

// Common helper methods
String.prototype.toHtml = function()
{
	var html = this.replace(/\n/g, "<br/>");
	html = html.replace(/\s/g, "&nbsp;");
	return html;
}


// MVC 
buco.mvc = new MVC(); 

// This class maintains a set of controller actions and defines the controller's base class
function MVC() { 	
	this.actions = []; 	
}

MVC.prototype.executeAction = function(action, params) { 
	var handler = this.actions[action]; 
	if(handler != null) {
		if(params == null) {
			handler();
		} else { 
			handler(params); 
		}
	} else {
		buco.log("No handler defined for action: " + action); 
	}
}

MVC.prototype.Controller = function(){
};

MVC.prototype.Controller.prototype.addAction = function(action, handler) {	
	buco.mvc.actions[action] = handler; 
}

// Helper class 
buco.helper = new Helper(); 

function Helper() { 
}

// Get bundle from local composition 
Helper.prototype.getBundle = function(bundleID) { 
	for(var i=0; i < buco.composition.bundles.length; i++) { 
		if(buco.composition.bundles[i].bundleID == bundleID) { 
			return buco.composition.bundles[i]; 			
		}
	}	
	return null;
}

// Get wire from local composition 
Helper.prototype.getWire = function(from, to) { 			
	for(var i=0; i < buco.composition.wires.length; i++) { 
		if(buco.composition.wires[i].to == to && buco.composition.wires[i].from == from) { 
			return buco.composition.wires[i]; 			
		}
	}	
	return null; 
}